import React from 'react'
import {Tabs,Select,Table, Divider, Tag,Modal,Button,message} from 'antd';
import './index.less';
import axios from 'axios';
import {PATH,IP} from '../../../../utils/urls';
import {createHashHistory} from 'history';
const { TabPane } = Tabs;
const { Option } = Select;
import BreadcrumbCustom from "../../../../components/BreadcrumbCustom";

export default class SoftWareManager extends React.Component {
    columnsArray = [
        {
            title: 'id',
            dataIndex: 'key',
            key: 'key',
            render:(text)=>{
                let index = text+1;
                return <span>{index}</span>
            }
        },{
            title: 'AppId',
            dataIndex: 'id',
            key: 'id',
        },{
            title: '类型',
            dataIndex: 'resouceTypeName',
            key: 'resouceTypeName',
        },{
            title: '所属设备',
            dataIndex: 'hardwareId',
            key: 'hardwareId',
        },{
            title: '名称',
            dataIndex: 'resouceName',
            key: 'resouceName',
        },{
            title: '开启采集',
            dataIndex: 'isCollect',
            key: 'isCollect',
            render:(text,record)=>{
                if(text == "1"){
                    return <span>未开启</span>
                }else{
                    return <span>开启</span>
                }
            }
        },,{
            title: '开启看护',
            dataIndex: 'isWatch',
            key: 'isWatch',
            render:(text,record)=>{
                if(text == "1"){
                    return <span>未开启</span>
                }else{
                    return <span>开启</span>
                }
            }
        },{
            title: '操作',
            key: 'tags',
            dataIndex: 'tags',
            render: (tags,data) => (
                <span>
            {tags.map((tag,index) => {
                if(index !=3){
                    return (
                        <span key={index}><a href="javascript:void(0);" onClick={this.actionClick.bind(this,{tag},{index},{data})}>{tag}</a>
                        <Divider type="vertical" /></span>
                    );
                }else{
                    return (
                        <span key={index}><a href="javascript:void(0);" onClick={this.actionClick.bind(this,{tag},{index},{data})}>{tag}</a></span>
                    );
                }
            })}
          </span>
            ),
        },
    ];
    constructor(props) {
        super(props);
        console.log(props)
        console.log(props.location);
        let appId = "";
        let name = "";
        let currentId = localStorage.getItem("softIdCurrent");
        if(props.location.state){
            appId = props.location.state.appId;
        }else if(currentId){
            appId = currentId;
        }
        if(props.location.state){
            name = props.location.state.name;
        }
        this.state = {
            tabsPostion:"1",//tabs选中索引
            hardWareArray:[],//硬件资源
            softWareArray:[],//软件资源
            hardWareId:appId,
            name:name
        };
    }

    componentDidMount() {
        //获取软件列表
        this.getSoftWareResource();
    }

    /**
     * 获取软件列表
     * @returns {*}
     */
    getSoftWareResource(){
        axios.post(PATH.softwareResource, {
            hardwareId: this.state.hardWareId
        }).then(response => {
            console.log(response);
            if (response.data.code == 0) {
                let dataArray = response.data.data;
                console.log(response.data.data);
                for(let i = 0;i<dataArray.length;i++){
                    dataArray[i].key = i;
                    dataArray[i].name = this.state.name;
                    //0未开启，1：开启，2：未发现
                    if(dataArray[i].serviceStatus == 0){
                        dataArray[i].tags = ["修改","启动","删除","重启"];
                    }else if(dataArray[i].serviceStatus == 1){
                        dataArray[i].tags = ["修改","启动","删除","重启"];
                    }else{
                        dataArray[i].tags = ["修改","删除"];
                    }
                }
                console.log(dataArray);
                this.setState({
                    hardWareArray:dataArray
                })
            }
        })
    }

    /**
     * 点击操作功能
     * @returns {*}
     */
    actionClick = (props0,props1,props2) => {
        console.log(props0);
        console.log(props1);
        console.log(props2);
        let path = {
            state:{
                appId:props2.data.id,
                name:props2.data.resouceName,
                resouceType:props2.data.resouceType
            },
        }
        switch (props1.index) {
            //修改信息
            case 0:
                path.pathname='/editsoftwareresource';
                break;
            //启动
            case 1:
                Modal.confirm({
                    title: "提示",
                    content: '是否确认启动？',
                    onOk:this.hardWareSwitch.bind(this,props2.data.id,35),
                    okText: '确认',
                    cancelText: '取消',
                });
                break;
            //删除
            case 2:
                Modal.confirm({
                    title: "提示",
                    content: '是否确认删除？',
                    onOk:this.delectAreaSceneFun.bind(this,props2),
                    okText: '确认',
                    cancelText: '取消',
                });
                break;
            //重启
            case 3:
                Modal.confirm({
                    title: "提示",
                    content: '是否确认重启？',
                    onOk:this.hardWareSwitch.bind(this,props2.data.id,38),
                    okText: '确认',
                    cancelText: '取消',
                });
                break;
            default:
                break;
        }
        this.props.history.push(path);
    }


    /**
     * 设备开关机
     * dealTypeDictId:33开机 34关机 35重启机器 36启动服务 37关闭服务 38重启服务
     * isSoftware:0是设备1 是软件
     */
    hardWareSwitch(resourceId,dealTypeDictId){
        axios.post(PATH.warnDeal,{
            dealTypeDictId:dealTypeDictId,
            isSoftware:1,
            resourceId:resourceId
        }).then(response=>{
            if(response.data.code == 0){
                message.success(response.data.msg)
            }else{
                message.error(response.data.msg)
            }
        });
    }

    /**
     * 删除区域场景
     * @returns {*}
     */
    delectAreaSceneFun(props2){
        console.log(props2);
        axios.post(PATH.resourceGatherConfRemove+"?ids="+props2.data.id,{
        },{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(response=>{
            console.log(response);
            if(response.data.code == 0){
                message.success(response.data.msg);
            }else{
                message.error(response.data.msg);
            }
        })
    }

    /**
     * 添加软件资源
     * @returns {*}
     */
    addSoftWareResource = () => {
        let appId = "";
        if(this.state.hardWareArray.length>0){
            appId = this.state.hardWareArray[0];
        }
        let path={
            pathname:"editsoftwareresource",
            state:{
                appId:appId,
                hardWareId:this.state.hardWareId
            }
        }
        this.props.history.push(path);
    }
    /**
     * 返回
     * @returns {*}
     */
    backFun=()=>{
        let path={
            pathname:"equipmentmanager"
        }
        this.props.history.push(path);
    }
    render() {
        return (
            <div>
                <BreadcrumbCustom first="配置管理" second="软件资源配置" />
                <div className="resourceAddType">
                    <div className="resourceManagerTitle">
                        <span>设备资源管理</span>
                        <Divider type="vertical"/>
                        <span className="equipmentSelect">软件资源管理</span>
                    </div>
                    <Button className="softWareAddBtn" onClick={this.backFun} style={{display:this.state.selectOrButtonShow}}>返回</Button>
                    <Button className="softWareAddBtn softWareAddBtnLeft" onClick={this.addSoftWareResource} style={{display:this.state.selectOrButtonShow}}>添加软件资源</Button>
                </div>
                <Table className= "reasureManagerClass" columns={this.columnsArray} dataSource={this.state.hardWareArray} bordered />
            </div>
        )
    }
}